package com.future.offer;

import com.future.util.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: ��ָ Offer 54. �����������ĵ�k��ڵ�
 * ����һ�ö��������������ҳ����е� k ��Ľڵ��ֵ��
 *
 * @Author: future
 * @Date:2022/5/23:23:19
 */
public class Solution_054 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(1);
        root.left.right = new TreeNode(2);
        root.right = new TreeNode(4);
        int k = 1;
        System.out.println(kthLargest(root, k));
    }

    public static int kthLargest(TreeNode root, int k) {
        List<Integer> list = new ArrayList<>();
        process(root, list);
        return list.get(k - 1);
    }

    private static void process(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        process(root.right, list);
        list.add(root.val);
        process(root.left, list);
    }


}
